<?php

namespace app\admin\controller\jdy;

use app\common\controller\Backend;
use think\Session;

use app\admin\library\Auth;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

use think\Log;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


use fast\Http;
/**
 * 工作事项狀態變更記錄管理
 *
 * @icon fa fa-circle-o
 */
class Taskrecords extends Backend
{
    
    /**
     * Taskrecords模型对象
     * @var \app\admin\model\jdy\Taskrecords
     */
    protected $model = null;

    const TASK_ID = "__taskrecordstaskid__";

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\jdy\Taskrecords;
        $this->view->assign("remindTypeList", $this->model->getRemindTypeList());
        $this->view->assign("taskStatusList", $this->model->getTaskStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        $task_id = 0;
        $url = $_SERVER['HTTP_REFERER'];
        $tasktitle = "";
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $task_id = $urls[$size - 1];
            Session::set(self::TASK_ID, $task_id);

            $taskrecords = \app\admin\model\jdy\Taskrecords::get($task_id);
            if ($taskrecords) {
                $tasktitle = $taskrecords->title;
            }
        } else {
            Session::set(self::TASK_ID, 0);
        }

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($task_id > 0) {
                $total = $this->model
                    ->with(['task'])
                    ->where($where)
                    ->where('task_id', $task_id)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['task'])
                    ->where($where)
                    ->where('task_id', $task_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            } else {
                $total = $this->model
                    ->with(['task'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['task'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            }


            $list = collection($list)->toArray();
            foreach ($list as &$row) {
                $row['maintenanceuser'] = $this->getadminnickname($row['maintenanceuser_id']);
                $row['handleuser'] = $this->getadminnickname($row['handleuser_id']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        $this->view->assign("tasktitle", $tasktitle);

        return $this->view->fetch();
    }

    function getadminnickname($adminid)
    {
        $admin = \app\admin\model\Admin::get($adminid);
        if ($admin) {
            return $admin->nickname;
        }

        return "";
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isAjax()) {
            $this->success("Ajax请求成功", null, ['id' => $ids]);
        }

        $row['maintenanceuser'] = $this->getadminnickname($row['maintenanceuser_id']);
        $row['handleuser'] = $this->getadminnickname($row['handleuser_id']);
        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

}
